package impl;

import java.util.concurrent.ArrayBlockingQueue;

import components.QuasiComponentMessage;

public class MessageImpl extends QuasiComponentMessage {

  private ArrayBlockingQueue<Message> messageBox = new ArrayBlockingQueue<Message>(100);
  private final Medium medium;

  public MessageImpl(Medium medium) {
    this.medium = medium;
  }

   public void send(AgentRef agent, Message message) {
    medium.send(agent, message);
  }
  
  
  public void receive(Message message) {
    try {
      messageBox.put(message);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

 
  public Message getMessage() {
	 // System.out.println("getMessage");
	 return messageBox.poll();   
  }
  
  
  
  public void start() {
  }

  
  public Message waitMessage() {
	// TODO Auto-generated method stub
	try {
		 //System.out.println("message");
		 Message m=messageBox.take();
		 //System.out.println("message ok "+m);
	    return m;
	} catch (InterruptedException e) {
	 e.printStackTrace();
	 return null;
	 }
	}

}
